package com.wmlive.hhvideo.heihei.mainhome.widget;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.PointF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import cn.wmlive.hhvideo.R;

import com.wmlive.hhvideo.utils.CollectionUtil;
import com.wmlive.hhvideo.utils.KLog;
import com.wmlive.hhvideo.utils.imageloader.GlideLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by vhawk on 2017/5/25.
 */

public class MusicAnimView extends RelativeLayout {

    private String TAG = "MusicAnimView";

    @BindView(R.id.iv_music_image)
    ImageView ivMusicImage;
    @BindView(R.id.iv_music)
    ImageView ivMusic;
    @BindView(R.id.iv_phone)
    ImageView ivPhone;

    private ObjectAnimator musicImageAnimator;

    /**
     *
     */
    private float x;
    /**
     *
     */
    private float y;
    /**
     *
     */
    private int height;
    /**
     *
     */
    private int width;

    private List<AnimatorSet> animatorSetList = new ArrayList<>();

    /**
     * 动画是否初始化完毕
     */
    private boolean isAnimInit;

    public MusicAnimView(Context context) {
        super(context);
        init(context);
    }

    public MusicAnimView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MusicAnimView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        View view = View.inflate(context, R.layout.view_music_anim, this);
        ButterKnife.bind(this, this);
        initAnim();
    }

    /**
     * 初始化动画配置
     */
    private void initAnim() {
        musicImageAnimator = ObjectAnimator.ofFloat(ivMusicImage, "rotation", 0f, 360f);

        musicImageAnimator.setDuration(1000);
        musicImageAnimator.setRepeatMode(ValueAnimator.RESTART);
        musicImageAnimator.setRepeatCount(ValueAnimator.INFINITE);
        musicImageAnimator.setInterpolator(new LinearInterpolator());

        ivMusicImage.post(new Runnable() {
            @Override
            public void run() {

                x = ivMusicImage.getX();
                y = ivMusicImage.getY();
                height = ivMusicImage.getMeasuredHeight();
                width = ivMusicImage.getMeasuredWidth();
//                initMusicValueAnimator();

                if (CollectionUtil.isEmpty(animatorSetList)){
                    initComplexAnimator(ivMusic);
                    initComplexAnimator1(ivPhone);
                }

            }
        });

    }

    /**
     * 音符动画
     *
     * @param targetView
     */
    private void initComplexAnimator1(final ImageView targetView) {
        ValueAnimator phoneValueAnimator = ValueAnimator.ofObject(new BezierEvaluator(new PointF(x , y + height + 50)), new PointF(x + width , y + height ), new PointF(x-30, y));
//        mValueAnimator.setDuration(2000);
        phoneValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                //在次对目标view做修改
                PointF pointF = (PointF) animation.getAnimatedValue();
                targetView.setX(pointF.x - targetView.getWidth() / 2);
                targetView.setY(pointF.y - targetView.getHeight() / 2);
            }
        });

        phoneValueAnimator.setInterpolator(new LinearInterpolator());
        phoneValueAnimator.setRepeatCount(ValueAnimator.INFINITE);

        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(targetView, "alpha", 0.4f, 1.0f, 0.5f);
        alphaAnimator.setRepeatCount(ValueAnimator.INFINITE);

        ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(targetView, "rotation", -30, 30, -30);
        rotateAnimator.setRepeatCount(ValueAnimator.INFINITE);
        rotateAnimator.setInterpolator(new AccelerateDecelerateInterpolator());


        AnimatorSet mAnimatorPhoneSet = new AnimatorSet();
        mAnimatorPhoneSet.play(phoneValueAnimator).with(alphaAnimator).with(rotateAnimator);

        mAnimatorPhoneSet.setDuration(2000);
        phoneValueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                Log.v("vhawk", "onAnimationStart");
                targetView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                Log.v("vhawk", "onAnimationEnd");
                targetView.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        animatorSetList.add(mAnimatorPhoneSet);
    }


    /**
     * 音符动画
     *
     * @param targetView
     */
    private void initComplexAnimator(final ImageView targetView) {
        ValueAnimator phoneValueAnimator = ValueAnimator.ofObject(new BezierEvaluator(new PointF(x , y + height + 50)), new PointF(x + width + 10, y + height + 10), new PointF(x, y));
//        mValueAnimator.setDuration(2000);
        phoneValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                //在次对目标view做修改
                PointF pointF = (PointF) animation.getAnimatedValue();
                targetView.setX(pointF.x - targetView.getWidth() / 2);
                targetView.setY(pointF.y - targetView.getHeight() / 2);
            }
        });

        phoneValueAnimator.setInterpolator(new LinearInterpolator());
        phoneValueAnimator.setRepeatCount(ValueAnimator.INFINITE);

        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(targetView, "alpha", 0.4f, 1.0f, 0.5f);
        alphaAnimator.setRepeatCount(ValueAnimator.INFINITE);

        ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(targetView, "rotation", -30, 30, -30);
        rotateAnimator.setRepeatCount(ValueAnimator.INFINITE);
        rotateAnimator.setInterpolator(new AccelerateDecelerateInterpolator());


        AnimatorSet mAnimatorPhoneSet = new AnimatorSet();
        mAnimatorPhoneSet.play(phoneValueAnimator).with(alphaAnimator).with(rotateAnimator);

        mAnimatorPhoneSet.setDuration(2000);
        phoneValueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                Log.v("vhawk", "onAnimationStart");
                targetView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                Log.v("vhawk", "onAnimationEnd");
                targetView.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        animatorSetList.add(mAnimatorPhoneSet);
    }

    /**
     *
     */
    public void startAnim() {

        KLog.v(TAG, "animatorSetList.size() = " + animatorSetList.size());

        musicImageAnimator.start();
        for (int i = 0; i < animatorSetList.size(); i++) {
            animatorSetList.get(i).setStartDelay(i * 1000);
            animatorSetList.get(i).start();
        }
    }

    /**
     *
     */
    public void pauseAnim() {

        for (int i = 0; i < animatorSetList.size(); i++) {
            animatorSetList.get(i).end();
        }

    }

    /**
     *
     */
    public void stopAnim() {
        musicImageAnimator.cancel();
        musicImageAnimator.end();

        for (int i = 0; i < animatorSetList.size(); i++) {
            animatorSetList.get(i).end();
        }

    }

    public void setMusicUrl(String url){
        GlideLoader.loadCircleImage(url,ivMusicImage,R.drawable.bg_search_music_default);
    }
}
